package com.swfee.qinben;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.swfee.qinben.base.AppBaseActivity;
import com.swfee.qinben.dto.active.MsgModel;
import com.swfee.qinben.dto.rsp.MessageRspDto;
import com.swfee.qinben.httpactions.MessageAction;

public class MyMsgListActivity extends AppBaseActivity {

	private LayoutInflater inflater;
	Handler handle = new Handler();
	private String curTypeCode;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		setContentView(R.layout.msg_detail_list);
		//
		// 加载数据
		//
		// 紧急消息：URGENTMSG
		// 管理员消息：ADMINMSG
		// 健康信息消息：HEALTHMSG
		// 医嘱消息：DOCTORMSG
		// 家属消息：FAMILYMSG
//		String[] typeCodes = "URGENTMSG,ADMINMSG,HEALTHMSG,DOCTORMSG,FAMILYMSG"
//				.split(",");

		curTypeCode = (String) getIntent().getStringExtra("typeName");
//		curTypeCode = typeCodes[tpIndex];

		Log.d("MyMsgListActivity--curTypeCode", curTypeCode + ":"+curTypeCode);
		asynInitData();

	}

	public void asynInitData() {

		showProgressDialog("加载数据中，请稍候..");
		new Thread(new Runnable() {

			@Override
			public void run() {
				List<MessageRspDto> msgLists = new MessageAction()
						.getMessages(curTypeCode);

				final List<MsgModel> list = new ArrayList<MsgModel>();

				for (int i = 0; i < msgLists.size(); i++) {
					MessageRspDto dto = msgLists.get(i);
					list.add(new MsgModel(dto.getMessageType(), dto
							.getContent(), dto.getSubmitDateTime()));
				}

				handle.post(new Runnable() {

					@Override
					public void run() {
						hideProgressDialog();
						ListView listView = (ListView) findViewById(R.id.listView);
						MsgModelListAdapter adapter = new MsgModelListAdapter(list);
						listView.setAdapter(adapter);

					}
				});

			}
		}).start();

	}

	// 适配器
	private final class MsgModelListAdapter extends BaseAdapter {

		private List<MsgModel> list;

		MsgModelListAdapter(List<MsgModel> list) {
			this.list = list;

		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.msg_detail_include,
						null);
				TextView msgTime = (TextView) convertView
						.findViewById(R.id.msg_time);
				TextView msgContent = (TextView) convertView
						.findViewById(R.id.msg_content);
				MsgModel model = list.get(position);
				msgTime.setText(model.getMsgTime());
				msgContent.setText(model.getMsgContent());

			}
			return convertView;
		}

	}

}